/*
重庆时时彩 二星
*/
cc.Class({
    extends: cc.Component,

    properties: {
        rtDec:cc.RichText,//说明
        betContent:[cc.Node],//投注选择面板
        ndMiss:[cc.Node],//遗漏显示
        fbSelBall:cc.Prefab,//选择球
        sscPop:cc.Node,//根节点

        rules:[],//规则
        nums:[],//数值
        _curTopTitle:"二星直选",//当前玩法名
        _lotteryID:0,//彩种id
        _isMiss:false,//是否开启遗漏
        _curSelects:[],//选择球数字记录
        _decContents:[],//说明列表
        _sscManage:null,
        _curPage:0,
  
    },

     //重置初始值
    initReset:function(){

    },

    //外部传参
    init:function(lotteryid){
        this._lotteryID = lotteryid;  
    },

    showMiss:function(data){
        for(var i = 0;i<this.ndMiss.length;i++)
        {
            var childs = this.ndMiss[i].children;
            for(var j=0;j<childs.length;j++)
            {
                if(j<data[i].length)
                {
                    childs[j].getComponent(cc.Label).string = data[i][j];
                }
            }
            this.ndMiss[i].active = true;
        }
    },

    hideMiss:function(){
        for(var i = 0;i<this.ndMiss.length;i++)
        {
            this.ndMiss[i].active = false;
        }
    },

    // use this for initialization
    start: function () {
        this._sscManage = this.sscPop.getComponent(this.sscPop.name);
        if(this._sscManage == null && this.betContent.length != 3 )
        {
            ComponentsUtils.showTips("投注面板加载失败");
            return;
        }

        this.rules = [DEFINE.LOTTERYRULESSC.ZHI_TWO,DEFINE.LOTTERYRULESSC.ZU_TWO];
        this.nums = ["0","1","2","3","4","5","6","7","8","9"]; 
        this._decContents = [
            "<color=#ffffff>每位至少选1个号码，按位猜对开奖号后2位即中</c><color=#fcd346>100</color><color=#ffffff>元宝</color>",
            "<color=#ffffff>至少选2个号码，猜对开奖号后2位(顺序不限，不含对子)即中</c><color=#fcd346>50</color><color=#ffffff>元宝</color>",
        ];
    
        for(var i=0;i<3;i++)
        {
            var arry = [];
            this._curSelects.push(arry);
        }
        this._lotteryID = DEFINE.LOTTERY_ID.SSC_CQ;

        for(var i=0;i<this.betContent.length;i++)
        {
            for(var j = 0;j<this.nums.length;j++)
            {      
                var ball = cc.instantiate(this.fbSelBall);
                ball.getComponent(ball.name).init(j);
                WidgetExtUtils.addCheckEvent(ball,this.node,this.node.name,"onClickCallback",{num:j,type:i});
                this.betContent[i].addChild(ball);
            }
        }
    },

    onSelPageCallBack:function(toggle, customEventData){
        if(toggle.getComponent(cc.Toggle).isChecked)
        {
            this.rtDec.string = this._decContents[customEventData];
            this._curPage = customEventData;
        }  
    },

    onClickCallback:function(toggle, customEventData){
        var num = customEventData.num;
        var type = customEventData.type;
        if(toggle.getComponent(cc.Toggle).isChecked)
        {
            this._curSelects[type].push(num);
        }
        else
        {
             Utils.removeByValue(this._curSelects[type],num);
        }
        this.updateTgSelect();
        this.checkBet();
    },

        //检查倍数
    checkBet:function(){
        var bet = 0;
        if(this._curPage == 0)//直选
        {   
            bet = this._curSelects[0].length*this._curSelects[1].length;
        }
        else if(this._curPage == 1)//组选
        {
            bet = LotteryUtils.combination(this._curSelects[2].length,2);
        }
        this._sscManage.showAmount(bet);
    },

    //清空当前选择
    clearAllBetRecord:function(){
        for(var i=0;i<this._curSelects.length;i++)
        {
            this._curSelects[i].length = 0;
        }

        for(var i=0;i<this.betContent.length;i++)
        {
            var childs = this.betContent[i].children;
            for(var j=0;j<childs.length;j++)
            {
                childs[j].getComponent(cc.Toggle).isChecked = false;
            }
        }
        this.checkBet();
    },

    //机选
    randomSelectCallBack:function(){
        this.clearAllBetRecord();
         if(this._curPage == 0)//直选
         {
            var randomArray = Utils.getRandomArrayWithArray(this.betContent[0].children, 1);
            if(randomArray.length>0)
            {
                randomArray[0].getComponent(cc.Toggle).isChecked = true;
                var temp = randomArray[0].getComponent(cc.Toggle).checkEvents[0].customEventData;
                this.onClickCallback(randomArray[0],temp);
            }
            randomArray = Utils.getRandomArrayWithArray(this.betContent[1].children, 1);
            if(randomArray.length>0)
            {
                randomArray[0].getComponent(cc.Toggle).isChecked = true;
                var temp = randomArray[0].getComponent(cc.Toggle).checkEvents[0].customEventData;
                this.onClickCallback(randomArray[0],temp);
            }
         }
         else if(this._curPage == 1)//组选
         {
            var randomArray = Utils.getRandomArrayWithArray(this.betContent[2].children, 2);
            for(var i=0;i<randomArray.length;i++)
            {
                randomArray[i].getComponent(cc.Toggle).isChecked = true;
                var temp = randomArray[i].getComponent(cc.Toggle).checkEvents[0].customEventData;
                this.onClickCallback(randomArray[i],temp);
            }
         }
        return true;
    },

    updateTgSelect:function(){
        for(var i=0;i<this._curSelects.length;i++)
        {
            if(this._curSelects[i].length > 0)
            {
                window.Notification.emit("updateTgSelect",false);
                return;
            }
        }
        window.Notification.emit("updateTgSelect",true);
    },

    //投注信息组合
    dataObj:function(){
        var objs = [];
        //组合
        var obj = new Object(); 
        obj.PlayCode = parseInt(this._lotteryID + this.rules[this._curPage]); 
        var nums = "";
        if(this._curPage == 0)//直选
        {
            Utils.sortNum(this._curSelects[0]);
            Utils.sortNum(this._curSelects[1]);
            for(var i=0;i<this._curSelects[0].length;i++)
            {
                if(nums != "")
                    nums += ",";
                nums += this._curSelects[0][i];
            }
            var num = "";
            for(var i=0;i<this._curSelects[1].length;i++)
            {
                if(num != "")
                    num += ",";
                num += this._curSelects[1][i];
            }
            nums = nums + "|" + num;
        }
        else if(this._curPage == 1)
        {
            Utils.sortNum(this._curSelects[2]);
            for(var i=0;i<this._curSelects[2].length;i++)
            {
                if(nums != "")
                    nums += ",";
                nums += this._curSelects[2][i];
            }
        }

        obj.num = nums;
        return obj;
    },
    
});
